/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kiakaku.downloadpage;

import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

/*
 *   File NewClass is created by kiakaku on Feb 16, 2012 11:05:52 AM
 * If you want modify this file please insert information in here 
 * - Last modify in Feb 16, 2012, 11:05:52 AM by kiakaku
 *                  
 *
 * @author kiakaku
 */
public class NewClass {

    List<String> outURL;
    String currURL;

    public NewClass(String currURL) {
        this.currURL = currURL;
        this.outURL = new ArrayList<String>();
    }

    public List<String> getOutURL() {
        try {
            URL url = new URI(currURL).toURL();
            URLConnection conn = url.openConnection();
            InputStreamReader rd = new InputStreamReader(conn.getInputStream());

            EditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", true);
            kit.read(rd, doc, 0);

            HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
            while (it.isValid()) {
                SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();

                String link = (String) s.getAttribute(HTML.Attribute.HREF);
                char firstChar = link.toCharArray()[0];
                if (link != null) {
//                    if ('/' == firstChar) {
//                        link = this.currURL + link;
//                    } else if (link.length() >= 4 && "http".equalsIgnoreCase(link.substring(0, 5))) {
//                        if (!outURL.contains(link)) {
//                            outURL.add(link);
                            System.out.println(link);
//                        }
//                    }
                }
                it.next();
            }
        } catch (Exception ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void main(String[] argv) {
        new NewClass("http://dantri.com.vn").getOutURL();
    }
}
